package vn.appsmobi.activity;


import java.text.NumberFormat;

import vn.appsmobi.R;
import vn.appsmobi.loader.AppCommentsLoader;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ui.AppCommentsFragment;
import vn.appsmobi.ui.BaseFragment;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.Log;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AppCommentsActivity extends BaseActivity {
	String mAppname = "";
	String mAppid	= "";
	private BaseFragment mAllCommentFragment;
	private AppInfo mAppInfo;
	private TextView mAverageScoreView;
	private TextView mScoreCountView;
	private TextView[] mScorePercentage;
	private ProgressBar[] mScoreProgress;
	private boolean mCommentLoaded = false;
	private View mContainer;
	private ImageButton mEditComment;
	
	private FragmentManager mFragmentManager;
	
	private View.OnClickListener mEditlistener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {			
			Intent intent = new Intent(AppCommentsActivity.this,EditCommentActivity.class);
			intent.putExtra(Constant.INTENT_EXTRA_APP_ID, mAppid);
			AppCommentsActivity.this.startActivityForResult(intent,1);			
		}
	};
	
	
	protected void onCreate(Bundle paramBundle)
	  {
	    super.onCreate(paramBundle);
	    
	    setCustomContentView(R.layout.app_comments);
	    Intent intent = getIntent();
	    mAppname = intent.getStringExtra(Constant.INTENT_EXTRA_APP_NAME);
	    mAppid = intent.getStringExtra(Constant.INTENT_EXTRA_APP_ID);
	    mAppInfo = AppInfo.get(mAppid);
	    setHomeButtonEnable(true);
	    setBarTitle(mAppname);
	    initialize();
	    
	  }
	
		
	public void initialize()
	  {
		this.mContainer = findViewById(R.id.app_comments);
	    this.mAverageScoreView = ((TextView)this.mContainer.findViewById(R.id.average_score));
	    this.mScoreCountView = ((TextView)this.mContainer.findViewById(R.id.comment_total_count));
	    this.mScoreProgress = new ProgressBar[5];
	    this.mScoreProgress[4] = ((ProgressBar)findViewById(R.id.five_star_progress));
	    this.mScoreProgress[3] = ((ProgressBar)findViewById(R.id.four_star_progress));
	    this.mScoreProgress[2] = ((ProgressBar)findViewById(R.id.three_star_progress));
	    this.mScoreProgress[1] = ((ProgressBar)findViewById(R.id.two_star_progress));
	    this.mScoreProgress[0] = ((ProgressBar)findViewById(R.id.one_star_progress));
	    this.mScorePercentage = new TextView[5];
	    this.mScorePercentage[4] = ((TextView)findViewById(R.id.five_star_percentage));
	    this.mScorePercentage[3] = ((TextView)findViewById(R.id.four_star_percentage));
	    this.mScorePercentage[2] = ((TextView)findViewById(R.id.three_star_percentage));
	    this.mScorePercentage[1] = ((TextView)findViewById(R.id.two_star_percentage));
	    this.mScorePercentage[0] = ((TextView)findViewById(R.id.one_star_percentage));
	    this.mEditComment		 = (ImageButton)findViewById(R.id.edit_btn);
	    this.mEditComment.setOnClickListener(mEditlistener);
	    this.mFragmentManager = getSupportFragmentManager();
	    FragmentTransaction fragmentTransaction = this.mFragmentManager.beginTransaction();	    
	    this.mAllCommentFragment = ((BaseFragment)this.mFragmentManager.findFragmentByTag(Constant.FRAGMENT_TAG_COMMENT_ALL));
	    if (this.mAllCommentFragment == null)
	    {
	      this.mAllCommentFragment = new AppCommentsFragment();	     
	      ((AppCommentsFragment)this.mAllCommentFragment).setAppId(this.mAppid);
	      fragmentTransaction.add(R.id.fragment_container, this.mAllCommentFragment,Constant.FRAGMENT_TAG_COMMENT_ALL);
	    }
	    
	    if (!fragmentTransaction.isEmpty())
	     {
	    	fragmentTransaction.commitAllowingStateLoss();
	        getSupportFragmentManager().executePendingTransactions();
	     }
	  }
	
	
	private int[] calculatePercentages(int[] counts)
	  {
	    int total = 0;
	    int[] returnArr = new int[counts.length];
	    
	    for(int i=0 ; i < counts.length; i ++)
	    	total += counts[i];
	    for(int i=0 ; i < counts.length; i ++)
	    {
	    	returnArr[i] = Math.round(100.0F * counts[i] / total);
	    }
	    return returnArr;
	  }
	
	
	public void showCountsPerStarLevel(int[] counts)
	  {
		
	    if (counts == null)
	     return;
	    int total = 0;
	    int[] arrPercent = calculatePercentages(counts);
	    
	    for(int i=0 ; i < counts.length; i ++)
	    	total += counts[i];
	    
	    for(int i = 0 ; i < counts.length ; i ++)
	    {
	    	this.mScoreProgress[i].setProgress(Math.round(100.0F * counts[i] / total));
	    	 this.mScorePercentage[i].setText(arrPercent[i] + "%");
	    }
	  }
	
	 public void updateCommentSummary(AppCommentsLoader.Result result)
	  {	   

	    showCountsPerStarLevel(result.mCountOfPoints);
	    if (result.mScore == 0)
	    {
	    	this.mAverageScoreView.setText("0");
	    	this.mScoreCountView.setText("0");
	    }
	      
	    else
	    {
	      this.mScoreCountView.setText(NumberFormat.getNumberInstance().format(result.mScoreTotalCount));
	      //AppInfo.cacheOrUpdate(localAppInfo);
	      this.mAverageScoreView.setText(String.format("%.1f", Double.valueOf(result.mScore)));
	    }
	  }


	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Log.v("resultCode:" + resultCode);
		 super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			
			//((AppCommentsFragment)this.mAllCommentFragment).refreshData();
        }
	} 
	 
	 
}
